<?php
// +----------------------------------------------------------------------
// | 云南喵云科技
// +----------------------------------------------------------------------
// | 官方网站: https://www.meoyun.com
// +----------------------------------------------------------------------
// | Author: 喵大大
// +----------------------------------------------------------------------
// | Email: mmteen@sina.com
// +----------------------------------------------------------------------
// | Date: 2021/10/29/029
// +----------------------------------------------------------------------
// | Time: 11:15
// +----------------------------------------------------------------------
namespace App\WebSocket;

use EasySwoole\Redis\Redis;
use EasySwoole\Redis\Config\RedisConfig;
use App\Model\UserModel;
use App\JwtController;

class WebSocketEvents
{

    /**
     * 连接打开时
     * @param \Swoole\WebSocket\Server $server
     * @param \Swoole\Http\Request $request
     */
    public static function onOpen(\Swoole\WebSocket\Server $server, \Swoole\Http\Request $request)
    {
        $redis = new Redis(new RedisConfig());
        $jwt = new JwtController();

        if(empty($request->get)){
            $redis->set("question" .$request->fd, $request->fd);

            $user = UserModel::create()->where(['status' => 'online'])->get();
            $data["user"] = $user;
            $data["type"] = "open";
            $data["user_fd"] = $request->fd;

            $server->push($request->fd, json_encode($data));

            //获取在线客服，通知有客户咨询
            $userList = UserModel::create()->where(['status' => 'online'])->all();

            foreach ($userList as $user){
                $workerFd = $redis->get("worker_" .$user["id"]);

                $message = [
                    "type" => "new_user",
                    "user_fd" => $request->fd,
                    "worker_uid" => $user["id"],
                    "content" => "有新的客服咨询"
                ];

                $server->push($workerFd, json_encode($message));
            }

        }else{
            $token = $request->get["token"];

            if (isset($token)) {
                $user = $jwt->checkToken($token);

                $redis = new Redis(new RedisConfig());
                $redis->set("worker_" .$user["id"], $request->fd);

                UserModel::create()->where('id', $user['id'])->update(['status' => 'online']);//标记为在线

                $message = [
                    "type" => "open",
                    "user_fd" => $request->fd,
                    "content" => "连接成功"
                ];

                $server->push($request->fd, json_encode($message));
            }
        }
    }

    /**
     * 连接关闭时
     * @param \Swoole\Server $server
     * @param int $fd
     * @param int $reactorId
     */

    public static function onClose(\Swoole\Server $server, int $fd, int $reactorId)
    {

        $redis = new Redis(new RedisConfig());
        $uid = $redis->get("user_" . $fd);

        UserModel::create()->where('id', $uid)->update(['status' => 'offline']);//标记为离线
    }
}